package com.atguigu.tingshu.user.strategy;

import java.util.Calendar;
import java.util.Date;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component(value = "1003")
@Slf4j
public class ProcessVipOrderTypePayRecord implements ProcessDiffPayRecord {
    @Autowired
    private UserVipServiceMapper userVipServiceMapper;
    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 处理vip订单类型支付记录
     *
     * @param userPaidRecordVo 订单信息
     */
    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        // user_vip_service表中插入数据
        String orderNo = userPaidRecordVo.getOrderNo();
        Long userId = userPaidRecordVo.getUserId();
        // 查询做幂等性校验
        Long count = userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>()
                .eq(UserVipService::getOrderNo, orderNo));
        if (count != 0) {
            throw new GuiguException(201, "该vip类型订单支付流水以存在");
        }
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(orderNo);
        userVipService.setUserId(userId);
        userVipService.setStartTime(new Date());

        // 查询用户购买Vip服务的套餐类型
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        // 获取购买套餐时间多长(月份)
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        // 查询用户身份判断其是否是Vip
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Integer isVip = userInfo.getIsVip();
        // 获取Vip过期时间
        Date vipExpireTime = userInfo.getVipExpireTime();
        // 获取日历对象（操作时期）
        Calendar instance = Calendar.getInstance();
//        if (isVip == 0) {
//            instance.setTime(new Date());
//        } else {
//            if (vipExpireTime.before(new Date())) {
//                instance.setTime(new Date());
//            } else {
//                instance.setTime(vipExpireTime);
//            }
//        }
        if (isVip == 0 || vipExpireTime.before(new Date())){
            instance.setTime(new Date());
        }else {
            instance.setTime(vipExpireTime);
        }
        instance.add(Calendar.MONTH, serviceMonth);
        userVipService.setExpireTime(instance.getTime());
        // 记录付款项类型是vip的支付流水
        int insert = userVipServiceMapper.insert(userVipService);
        log.info("保存订单：{}，记录支付流水:{}", orderNo, insert > 0 ? "成功" : "失败");
        //修改用户Vip相关信息(是否是Vip和过期时间)
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        int update = userInfoMapper.updateById(userInfo);
        log.info("修改用户信息{}", update > 1 ? "成功" : "失败");
    }
}
